1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect.testing.testers;
18
19 import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20 import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
21 import static com.google.common.collect.testing.features.CollectionFeature.RESTRICTS_ELEMENTS;
22 import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
23 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
24
25 import com.google.common.annotations.GwtCompatible;
26 import com.google.common.collect.testing.AbstractCollectionTester;
27 import com.google.common.collect.testing.features.CollectionFeature;
28 import com.google.common.collect.testing.features.CollectionSize;
29
30 import java.util.ConcurrentModificationException;
31 import java.util.Iterator;
32
33
34
35
36
37
38
39
40
41 @SuppressWarnings("unchecked")
42 @GwtCompatible(emulated = true)
43 public class CollectionAddTester<E> extends AbstractCollectionTester<E> {
44 @CollectionFeature.Require(SUPPORTS_ADD)
45 public void testAdd_supportedNotPresent() {
46 assertTrue("add(notPresent) should return true",
47 collection.add(samples.e3));
48 expectAdded(samples.e3);
49 }
50
51 @CollectionFeature.Require(absent = SUPPORTS_ADD)
52 public void testAdd_unsupportedNotPresent() {
53 try {
54 collection.add(samples.e3);
55 fail("add(notPresent) should throw");
56 } catch (UnsupportedOperationException expected) {
57 }
58 expectUnchanged();
59 expectMissing(samples.e3);
60 }
61
62 @CollectionFeature.Require(absent = SUPPORTS_ADD)
63 @CollectionSize.Require(absent = ZERO)
64 public void testAdd_unsupportedPresent() {
65 try {
66 assertFalse("add(present) should return false or throw",
67 collection.add(samples.e0));
68 } catch (UnsupportedOperationException tolerated) {
69 }
70 expectUnchanged();
71 }
72
73 @CollectionFeature.Require(
74 value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES},
75 absent = RESTRICTS_ELEMENTS)
76 public void testAdd_nullSupported() {
77 assertTrue("add(null) should return true", collection.add(null));
78 expectAdded((E) null);
79 }
80
81 @CollectionFeature.Require(value = SUPPORTS_ADD,
82 absent = ALLOWS_NULL_VALUES)
83 public void testAdd_nullUnsupported() {
84 try {
85 collection.add(null);
86 fail("add(null) should throw");
87 } catch (NullPointerException expected) {
88 }
89 expectUnchanged();
90 expectNullMissingWhenNullUnsupported(
91 "Should not contain null after unsupported add(null)");
92 }
93
94 @CollectionFeature.Require({SUPPORTS_ADD,
95 FAILS_FAST_ON_CONCURRENT_MODIFICATION})
96 @CollectionSize.Require(absent = ZERO)
97 public void testAddConcurrentWithIteration() {
98 try {
99 Iterator<E> iterator = collection.iterator();
100 assertTrue(collection.add(samples.e3));
101 iterator.next();
102 fail("Expected ConcurrentModificationException");
103 } catch (ConcurrentModificationException expected) {
104
105 }
106 }
107 }
108